package com.xinweisi.dataserver.module.datastatistics.behaviordaily;

import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.datastatistics.behaviordaily.domain.dto.BehaviorDailyQueryDTO;
import com.xinweisi.dataserver.module.datastatistics.behaviordaily.domain.vo.BehaviorDailyDataVO;
import com.xinweisi.dataserver.util.DBNameUtil;
import com.xinweisi.dataserver.util.DateUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.text.ParseException;
import java.util.Date;

/**
 * BehaviorDailyController
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/27 0027 11:09
 */
@Slf4j
@RestController
@RequestMapping("/api/sysData/behaviorDaily")
public class BehaviorDailyController {
    @Value("${startDBName}")
    private String OldDBName;

    @Autowired
    private BehaviorDailyService behaviorDailyService;

    @ApiOperation(value = "行为日报", notes = "行为日报")
    @RequestMapping(value = "/getData",method= RequestMethod.POST)
    public ResponseDTO<BehaviorDailyDataVO> getData(@RequestBody @Valid BehaviorDailyQueryDTO queryDTO) throws ParseException {
        log.info("----------行为日报----------");
        String startTime;
        if(queryDTO.getStartTime()!=null && !"".equals(queryDTO.getStartTime())){
            startTime = queryDTO.getStartTime();
        }else {
            startTime = DateUtil.getDate(new Date());
            queryDTO.setStartTime(startTime);
        }
        String endTime;
        if(queryDTO.getEndTime()!=null && !"".equals(queryDTO.getEndTime())){
            endTime = queryDTO.getEndTime();
        }else {
            endTime =queryDTO.getStartTime();
        }

        queryDTO.setDBNameList(DBNameUtil.getDANameList(OldDBName,startTime,endTime));
        return behaviorDailyService.getData(queryDTO);
    }
}
